package com.tjsinfo.tjsoa.worklog.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogDefineBo;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogExamineBo;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogMainBo;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogDefine;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogMain;
public class InstructionWorkLogAction extends Action{
	private IWorkLogExamineBo workLogExamineBo;
	private IWorkLogMainBo workLogMainBo;
	private IWorkLogDefineBo workLogDefineBo;
	public IWorkLogDefineBo getWorkLogDefineBo() {
		return workLogDefineBo;
	}


	public void setWorkLogDefineBo(IWorkLogDefineBo workLogDefineBo) {
		this.workLogDefineBo = workLogDefineBo;
	}


	public IWorkLogExamineBo getWorkLogExamineBo() {
		return workLogExamineBo;
	}


	public void setWorkLogExamineBo(IWorkLogExamineBo workLogExamineBo) {
		this.workLogExamineBo = workLogExamineBo;
	}


	public IWorkLogMainBo getWorkLogMainBo() {
		return workLogMainBo;
	}


	public void setWorkLogMainBo(IWorkLogMainBo workLogMainBo) {
		this.workLogMainBo = workLogMainBo;
	}


	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String jihuaid = request.getParameter("id");
			Integer messageid = Integer.parseInt(request.getParameter("messageid"));
			if(messageid == 0)
			{
				//tjsMessageBo.deletetjsmessagebyid("工作日志 ", jihuaid,user.getUser_id());
			}
			else
			{
				//tjsMessageBo.deletetjsmessagebyid(messageid);
			}
			
	
		String value = request.getParameter("contest");
		if (workLogExamineBo.saveORupdateWorkLog(user.getUser_id(), jihuaid, value)) {
			WorkLogMain workLogMain = workLogMainBo.selectByID(jihuaid);
			workLogMain.setStart(2);
			workLogMainBo.update(workLogMain.getId(), workLogMain.getTjsUser().getUserId(), workLogMain.getTime(), workLogMain.getType(), workLogMain.getContent(), workLogMain.getCompleteContent(), workLogMain.getYear(), workLogMain.getNumber(), workLogMain.getStart());
			List list =	workLogDefineBo.selectDefine(user.getUser_id());
			 com.tjsinfo.tjsoa.worklog.sqlmap.WorkLogDefine workLogDefine = new com.tjsinfo.tjsoa.worklog.sqlmap.WorkLogDefine();
			 workLogDefine.setTjsUserByUserId(user.getUser_id());
			 workLogDefine.setType("D");
		
				/*TjsMessage tjsMessage = new TjsMessage();
				 tjsMessage.setColname("工作日志 ");
				 tjsMessage.setValue(user.getUsername()+"批示了一个您工作日志,请查看!");
				 tjsMessage.setTitle(user.getUsername()+"批示了一个您工作日志,请查看!");
				 String path = request.getContextPath();
				 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/tjsinfo/tjsoa/worklog/selectWorkLog.do?id="+jihuaid;	*/
			int i = 1;
			if(list.size() == 0)
			{
				return mapping.findForward("oksuddenly");
			}
		/*	for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				WorkLogDefine workLogDefine = (WorkLogDefine) iterator.next();
				Integer id = workLogMain.getTjsUser().getUserId();
				if(workLogDefine.getUserId().equals(id))
				{
					break;
				}
				else if (!workLogDefine.getTjsUser().getUserId().equals(id)&& i==list.size()) {
					return mapping.findForward("oksuddenly"); 
				}
				i++;
			}*/
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				TSystemUser workLogDefine2 = (TSystemUser) iterator.next();
				Integer id = workLogMain.getTjsUser().getUserId();
				if(workLogDefine2.getUserId().equals(id))
				{
					break;
				}
				else if (!workLogDefine2.getUserId().equals(id)&& i==list.size()) {
					//return mapping.findForward("oksuddenly"); 
				}
				i++;
			}
			if(request.getParameter("temp") !=null)
			{
				RequestDispatcher   ds   =   request.getRequestDispatcher("checkSuddenlyWorkLog.do?id="+workLogMain.getTjsUser().getUserId()); 
				ds.forward(request,   response);
				return null;
			}
			else if("D".equals(workLogMain.getType()))
			{
				/*RequestDispatcher   ds   =   request.getRequestDispatcher("checkDefalntWorkLogDateModle.do?id="+workLogMain.getTjsUser().getId()); 
				ds.forward(request,   response);*/
				request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/checkDefalntWorkLogDateModle.do?id="+workLogMain.getTjsUser().getUserId());
				request.setAttribute("message", "worklog.message.check");
				return mapping.findForward("link");
				// return mapping.findForward("okD");
			}
			else if("W".equals(workLogMain.getType()))
			{
				
				request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/checkDefalntWorkLogWeekModle.do?id="+workLogMain.getTjsUser().getUserId());
				request.setAttribute("message", "worklog.message.check");
				return mapping.findForward("link");
				//return mapping.findForward("okW");
			}
			else if("M".equals(workLogMain.getType()))
			{
			
				request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/checkDefalntWorkLogMonthModle.do?id="+workLogMain.getTjsUser().getUserId());
				request.setAttribute("message", "worklog.message.check");
				return mapping.findForward("link");
				//return mapping.findForward("okM");
			}
			else if("Q".equals(workLogMain.getType()))
			{
				
				request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/checkDefalntWorkLogMonthJiDuModle.do?id="+workLogMain.getTjsUser().getUserId());
				request.setAttribute("message", "worklog.message.check");
				return mapping.findForward("link");
				//return mapping.findForward("okQ");
				
			}
			else if("H".equals(workLogMain.getType()))
			{
				
				request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/checkworkLogHalfYearModle.do?id="+workLogMain.getTjsUser().getUserId());
				request.setAttribute("message", "worklog.message.check");
				return mapping.findForward("link");
				//return mapping.findForward("okH");
			}
			else if("Y".equals(workLogMain.getType()))
			{
				
				request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/checkworkLogYearModle.do?id="+workLogMain.getTjsUser().getUserId());
				request.setAttribute("message", "worklog.message.check");
				return mapping.findForward("link");
				//return mapping.findForward("okY");
			}
			else if("list".equals(workLogMain.getType()))
			{
				return mapping.findForward("oklist");
			}
			return mapping.findForward("okY");
		} else {
			return mapping.findForward("error");
		}
		
	}
	
}
